package game.mouse
{
	import editor.UI.EditorUI;
	import flash.display.BitmapData;
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.geom.Point;
	import game.manager.ConfigurationManager;
	import game.map.PhysicalMap;
	import game.map.VisualMap;
	import game.VO.TileTemplateVO;
	import game.VO.TileVO;
	import starling.display.Image;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.textures.Texture;
	
	/**
	 * ...
	 * @author Kirill Pletnev
	 */
	public class MouseCursor
	{
		private var _visualMap:VisualMap;
		private var _cursor:Image;
		
		public function MouseCursor(visualMap:VisualMap)
		{
			_visualMap = visualMap;
			_visualMap.addEventListener(TouchEvent.TOUCH, onTouch);
			_cursor = generateMapCursor(ConfigurationManager.instance.mapCursorColor, ConfigurationManager.instance.mapCursorAlpha);
			_visualMap.addChild(_cursor);
		}
		
		/**
		 * создание подсветки тайла игравой карты
		 */
		private static function generateMapCursor(color:uint, alpha:Number):Image
		{
			var shape:Shape = createLightTile(color, alpha);
			
			var bitmapData:BitmapData = new BitmapData(shape.width, shape.height, true, 0);
			bitmapData.draw(shape);
			
			var texture:Texture = Texture.fromBitmapData(bitmapData);
			var image:Image = new Image(texture);
			image.pivotX = image.width >> 1;
			
			shape = null;
			bitmapData.dispose();
			
			return image;
		}
		
		/**
		 * создание подсветки тайла игравой карты
		 */
		public static function createLightTile(color:uint, alpha:Number):Shape
		{
			var shape:Shape = new Shape();
			
			var g:Graphics = shape.graphics;
			g.beginFill(color, alpha);
			g.moveTo(0, ConfigurationManager.instance.tileHalfHeight);
			g.lineTo(ConfigurationManager.instance.tileHalfWidth, 0);
			g.lineTo(ConfigurationManager.instance.tileWidth, ConfigurationManager.instance.tileHalfHeight);
			g.lineTo(ConfigurationManager.instance.tileHalfWidth, ConfigurationManager.instance.tileHeight);
			g.lineTo(0, ConfigurationManager.instance.tileHalfHeight);
			g.endFill();
			
			return shape;
		}
		
		public function release():void
		{
			_visualMap.removeEventListener(TouchEvent.TOUCH, onTouch);
		}
		
		private function onTouch(event:TouchEvent):void
		{
			if (event.touches.length == 1)
			{
				var touches:Touch = event.touches[0];
				var point:Point = touches.getLocation(_visualMap);
				var column:int = VisualMap.getColumn(point.x, point.y);
				var row:int = VisualMap.getRow(point.x, point.y);
				
				if (touches.phase == TouchPhase.HOVER)
				{
					if (column >= 0 && row >= 0 && column < _visualMap.numColumns && row < _visualMap.numRows)
					{
						_cursor.x = VisualMap.getX(column, row);
						_cursor.y = VisualMap.getY(column, row);
					}
					return;
				}
				else if (touches.tapCount == 2 && touches.phase == TouchPhase.ENDED)
				{
					if (ConfigurationManager.instance.isEditor)
					{
						var tileTemplateVO:TileTemplateVO = EditorUI.instanse.rightPanel.selectedTileTemplateVO;
						if (tileTemplateVO != null)
						{
							var tileVO:TileVO = new TileVO(tileTemplateVO.id, column, row);
							_visualMap.tilesContainer.addTile(tileVO);
						}
						else
						{
							var tileIndex:int = PhysicalMap.getTileIndex(column, row, _visualMap.numColumns, _visualMap.numRows);
							_visualMap.tilesContainer.removeTile(tileIndex);
						}
					}
					trace("Double-clicks on column = " + column + " and row = " + row);
					return;
				}
			}
		}
	}

}